package gw.com.android.presenter.http;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import gw.com.android.app.AppMain;
import gw.com.android.model.ConfigUtil;
import gw.com.android.utils.CacheUtils;
import okhttp3.Request;
import okhttp3.Response;
import www.com.library.app.Logger;
import www.com.library.interfaces.ReqCallBack;

/**
 * 平台运营数据
 *
 * @author hank
 * @since 2018-8-2
 */
public class HomePlatformOperationDataRequest {

    private String cacheFileName = "_operation_data.dat";

    public PlatformOperationData fetchLocal() {
        String str = CacheUtils.readFile(cacheFileName);
        return parse(str, false);
    }

    public void call(final ReqCallBack<PlatformOperationData> callBack) {
        String mUrl = ConfigUtil.instance().getUrlPath("getWwwDatas");
        new Https(mUrl).responseOnUiThread(false).get(new Https.ResponseCallback<String>() {
            @Override
            public void onSuccess(Request request, Response response, String result, int code) {
                PlatformOperationData data = parse(result, true);
                postSuccess(data, callBack);
            }

            @Override
            public void onFailure(Request request, IOException e) {
                postFailure(e, callBack);
            }
        });
    }

    private void postSuccess(final PlatformOperationData data, final ReqCallBack<PlatformOperationData> callBack) {
        AppMain.getApp().getHandler().post(new Runnable() {
            @Override
            public void run() {
                callBack.onReqSuccess(data);
            }
        });
    }

    private void postFailure(Exception e, final ReqCallBack<PlatformOperationData> callBack) {
        final String msg = e == null ? "" : e.getMessage();
        AppMain.getApp().getHandler().post(new Runnable() {
            @Override
            public void run() {
                callBack.onReqFailed(msg);
            }
        });
    }

    private void storeLocal(String str) {
        CacheUtils.writeFile(cacheFileName, str);
    }

    private PlatformOperationData parse(String str, boolean cache) {
        Log.d("PlatformOperationData", "" + str);
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(str);
            JSONObject dataJson = jsonObject.optJSONObject("data");
            if (dataJson != null) {
                PlatformOperationData data = new PlatformOperationData();
                data.workTime = dataJson.optString("workTime");//形如“2933,14:00:51”，但是我要转化成2933
                data.tradesCount = dataJson.optString("tradesCount");
                data.registersCount = dataJson.optString("registersCount");
                if (cache) {
                    storeLocal(str);
                }
                return data;
            }
        } catch (JSONException e) {
            Logger.e(e);
        }
        return null;
    }

    public class PlatformOperationData {
        public String workTime;//平台运营天数
        public String tradesCount;//累计交易笔数
        public String registersCount;// 已服务用户数
    }

}
